home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2784 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Floating point calculation order
  5. Date: Tue, 23 Jan 1996 22:11:22 GMT
  6. Organization: Netcom
  7. Message-ID: <310554a1.170357056@nntp.ix.netcom.com>
  8. References: <m0tedv8-0002eqC@sice.nsk.su> <3104c6d9.134061184@nntp.ix.netcom.com> <DLnE5K.2xH@microunity.com> <mjs.822427333@hubcap>
  9. NNTP-Posting-Host: ix-dc18-03.ix.netcom.com
  10. X-NETCOM-Date: Tue Jan 23  2:10:41 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. mjs@hubcap.clemson.edu (M. J. Saltzman) wrote:
  14.  
  15. > toms@MicroUnity.com (Tom Sanders) writes:
  16. > %In article <3104c6d9.134061184@nntp.ix.netcom.com>, miker3@ix.netcom.com (Mike Rubenstein) writes:
  17. > %|> "Pavel A. Zemtsov" <PZEM@sice.nsk.su> wrote:
  18. > %|> >  Having declaration
  19. > %|> > 
  20. > %|> >  double x, p, q, r;
  21. > %|> > 
  22. > %|> > my compiler (cc on SCO 3.2) calculated following expression:
  23. > %|> > 
  24. > %|> >  x = p * q / r;
  25. > %|> > 
  26. > %|> > as p * (q/r); (I mean, it divided first, than multiplied). That 
  27. > %|> > resulted in precision loss.
  28. > %|> > 
  29. > %|> > Is this legal behavior?
  30. > %|> 
  31. > %|> Not in ANSI C, but it was legal in K&R. If memory serves, it was not
  32. > %|> made illegal until very late in the standardizattion process so if
  33. > %|> your compiler was written before the standard was finalized it may not
  34. > %|> adhere to this.
  35. > %I am a firm believer in not allowing a compiler to make these decisions for
  36. > %me.  It's best to specify with parens the order of evaluation and not leave
  37. > %it up to the compiler defaults.  I would actually prefer a compiler that
  38. > %gave a warning if I did have an equation that could be evaluated in more
  39. > %than 1 order (something I have not done in over 20 years).
  40. > As I understand it, before ANSI, the compiler was free to even ignore
  41. > parentheses and reorder the evaluation as long as the result was
  42. > *mathematically* equivalent to the original expression.  So the only
  43. > way to force an evaluation order was to use multi-line code with
  44. > temporaries to hold the intermediate results.
  45.  
  46. This is correct.  Under K&R the only way to assure order of evaluation
  47. was to break it up into separate statements.
  48.  
  49. > Would somebody explain under exactly what circumstances an ANSI
  50. > compiler is and is not allowed to reorder evaluation?  My recollection
  51. > was that parentheses forced evaluation order, but leaving them off
  52. > allowed the compiler the freedom to reorder.  That's not what 
  53. > Rubenstein is claiming, though.
  54. > Thus, in
  55. >     x = p * q / r;
  56. > the compiler is legally allowed to do the division first, but in
  57. >     x = (p * q) / r;
  58. > it is not, so that Sanders's strategy is effective in this case.
  59.  
  60. Not quite.  The compiler may never alter the order of evaluation
  61. insofar as it is specifed in the standard except, of course, under the
  62. as if rule, it may alter a program in any way as long as the result is
  63. the same.
  64.  
  65.     x = p * q / r;
  66.  
  67. and
  68.  
  69.     x = (p * q) / r;
  70.  
  71. are equivalent.  In either case, the compiler must determin the value
  72. of p * q and r and then do the division.  Note that there is no
  73. required order on the evaluations of  p, q, and r.
  74.  
  75. > I also recall that the unary + was supposed to be useful in enforcing
  76. > evaluation order, but I can't recall how it fits in.
  77.  
  78. This was proposed as part of the standard, but was dropped in favor of
  79. disallowing changing the order completely.  As I said, if memory
  80. serves this was very late in the standardization process and I
  81. wouldn't count on it from any compiler written before the standard was
  82. finalized even if it appears to be standard.
  83.  
  84.  
  85. Michael M Rubenstein
  86.